<?php

class Admin_Model_Admin extends Zend_Db_Table_Abstract
{
    protected $_name = 'admin';
    public function getAllData(){
    	$query = $this->select()
                        ->from($this->_name)
                        ->where("id > 1");
    	return $this->fetchAll($query);
    }
    public function getDataById($id){
    	$query = $this->select()
                        ->from($this->_name)
                        ->where('id = ?',(int)$id);
        return $this->fetchRow($query);
    }
    public function addData($username, $password, $fullname, $status, $role){
    	$data = array('username'    => $username,
                    'password'      => md5($password),
                    'fullname'         => $fullname,                            
                    'status'        => $status,                            
                    'role'          => $role);        
        $idAdd = $this->insert($data);
        return $idAdd;
    }
    public function deleteData($id){
        $this->delete('id =' . (int)$id);  
    }
    public function updateProfile($id, $username, $fullName, $avatar){
		 $data = array( 'username' => $username,
                        'fullname' => $fullName,
                        'avatar' => $avatar);  
        return $this->update($data,'id = ' . (int)$id);
    }  
    public function updateData($id, $username, $password, $fullname, $status, $avatar){
    	if($password && strlen($password) >= 6){
    		 $data = array('username' => $username,
                            'password'=> md5($password),                            
                            'fullname'   => $fullname,
                            'status'  => $status,
                            'avatar'  => $avatar);
    	}else{    	       
    		 $data = array('username' => $username,                                                        
                            'fullname' => $fullname,
                            'status' => $status,
                            'avatar' => $avatar);     
       	}        
        return $this->update($data,'id = ' . (int)$id);
    }    
    public function updatePass ($id, $password){
        $data['password'] = md5($password);
        return $this->update($data, 'id=' . (int)$id );
    }
    public function getDataByUsername($username){
        $query = $this->select()
                        ->from($this->_name)
                        ->where('username = ?', $username);
        return $this->fetchRow($query);
    }     
}

